#ifndef TOOLS_H
#define TOOLS_H
#include <QMainWindow>
#include <string>
#include <list>
#include <map>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <QRegExp>
#include <iostream>
#include "math.h"
#include "time.h"
using namespace std;
typedef int int32_t;
class wstr:public std::wstring
{
public:
    wstr(){};
    wstr(const wchar_t* a): std::wstring(a?a:L""){};
    wstr(const std::wstring& a):std::wstring(a){};
    void replace(const std::wstring& what,const std::wstring& to)
    {
        int pos;
        if(to.find(what)>=0)
        {
            if((pos=find(what.c_str()))>=0)
                wstring::replace(pos,what.length(),to);
        }
        else
        {
            while((pos=find(what.c_str()))>=0)
                wstring::replace(pos,what.length(),to);
        }

    }

};
class str:public std::string
{
public:
    str(){};
    str(const char* a): std::string(a?a:""){};
    str(const std::string& a):std::string(a){};
    int to_int(){return atof(c_str());}
    float to_float(){return atof(c_str());}
    str& operator+(int a)
    {
        char buf[24];
        sprintf(buf, "%i", a) ;
        (*this)+=buf;
        //(*this)+=" ";

        return (*this);
    }
    str(const int& a)
    {
        char buf[24];
        sprintf(buf, "%i", a) ;
        (*this)+=buf;

    }
    str(const unsigned long & a)
    {
        char buf[24];
        sprintf(buf, "%ul", a) ;
        (*this)+=buf;

    }
    str(const char& a):std::string("")
    {
        (*this)+=a;
    }
    str(const float& a)
    {
        char buf[24];
        sprintf(buf, "%8.5f", a) ;
        (*this)+=buf;

    }
    int find(str what)
    {
        return string::find(what.c_str());
    }
    void replace(str what ,str to)
    {
        int pos;
        if(to.find(what)>=0)
        {
            if((pos=find(what.c_str()))>=0)
                string::replace(pos,what.length(),to);
        }
        else
        {
            while((pos=find(what.c_str()))>=0)
                string::replace(pos,what.length(),to);
        }

    }
    str format(const char * formatstring, ...)
    {
        va_list va;

        va_start(va, formatstring);

        char buffer[1024];

        int32_t length = ::vsnprintf(buffer, sizeof(buffer), formatstring, va);

        if (length >= 0 && length < 1024)
        {
            *this= str(buffer);
            return *this;
        }

        va_end(va);

    }
    void append(str add,int k=0)
    {
        *this=*this + add;
    }

};

class ICallBack
{
public:
    virtual void OnComplete(void* data=0)=0;
    virtual ~ICallBack(){};
};
inline float RND(float _min,float _max)
{
    if(_min>_max)
        return _max;
    float rnd = (float)(rand()%10000);
    return (((_max-_min)/10000)*rnd)+_min;
};
bool IsEmail(const std::string email);


#endif // TOOLS_H
